package com.ofps.forms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.common.core.domain.entity.SysUser;
import com.ofps.forms.common.utils.JwtUtil;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.common.utils.SecretUtil;
import com.ofps.forms.common.utils.TokenUtil;
import com.ofps.forms.domain.Dto.UserDto;
import com.ofps.forms.mapper.*;
import com.ofps.forms.domain.*;
import com.ofps.forms.service.*;
import com.ofps.system.mapper.SysUserMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("userService")
public class MyUserServiceImpl extends ServiceImpl<MyUserMapper, MyUser> implements MyUserService {
    @Resource
    private MyUserMapper myUserMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    //注册方法
    @Override
    public R register(MyUser myUser) {
        //判断用户是否存在 存在返回false 重新选择用户名
        //前端检测空值
        if (null == myUser.getUser_name() || null == myUser.getPassword())
            return R.error("用户名或密码为空值");

        //查询语句初始化
        QueryWrapper<MyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", myUser.getUser_name());
        return null;
//        if (!myUserMapper.exists(queryWrapper)) {
//            //获取加密数据  加密后传回map包含password和salt
//            Map<String, String> hashMap = SecretUtil.Encryption(myUser.getPassword());
//            //set用户密码和盐值
//            myUser.setPassword(hashMap.get("password")).setSalt(hashMap.get("salt"));
//            //存入数据库
//            myUserMapper.insert(myUser);
//            return R.ok("注册成功！");
//        } else return R.error("用户名已存在");
    }

    //登录方法
    @Override
    public R login(UserDto userDto) {
        //null检测
        if (null == userDto || null == userDto.getUsername() || null == userDto.getPassword())
            return R.error("用户名或密码为空值");
        //根据传入dto对象 在数据库根据用户名查询密码(加密后)
        //与传入dto对象的密码进行比较
        QueryWrapper<MyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDto.getUsername());
        MyUser myUser = myUserMapper.selectOne(queryWrapper);

        if (null == myUser)
            return R.error("用户不存在");//query结果为null
        return null;
//        //调用SecretUtil校验  校验通过生成并返回token 放行拦截器
//        if (SecretUtil.checkEncryption(userDto.getPassword(), myUser.getSalt(), myUser.getPassword())) {
//            Integer id = myUser.getUser_id();
//            HashMap<String, String> payload = new HashMap<>();
//            payload.put("id", id.toString());
//            payload.put("username", userDto.getUsername());
//            String token = JwtUtil.getJWToken(payload);
//            return R.ok("登录成功").put("token", token);
//        } else return R.error("密码错误");//密码不匹配

    }

    @Override
    public R getInfo(HttpServletRequest request) {
        //用户id
//        Integer userId = TokenUtil.getId(request);
        Integer userId = Math.toIntExact(getUserId());
        //返回用户信息
        //用户名username  头像avatar  角色role[]  权限permissions[] (部门dept )

        //获取角色信息
        //根据用户id查询UserRole表 获取RoleId
//        List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id", userId));
//        List<RoleEntity> roles = new LinkedList<>();
//        List<Integer> roleIdList = new LinkedList<>();
//        for (UserRoleEntity userRole : userRoleEntities) {
//            roleIdList.add(userRole.getRoleId());
//            roles.add(roleDao.selectById(userRole.getRoleId()));
//        }
//
//        //获取权限
//        //根据roleId 查询 rolePermission表 获取permissionId List
//        List<RolePermissionEntity> rolePermissionList = new LinkedList<>();
//        for (Integer roleId : roleIdList) {
//            List<RolePermissionEntity> rolePermissionEntityList = rolePermissionDao.selectList(new QueryWrapper<RolePermissionEntity>().eq("role_id", roleId));
//
//            rolePermissionList.addAll(rolePermissionEntityList);
//        }
//        Set<PermissionEntity> permissions = new HashSet<>();
//        for (RolePermissionEntity rolePermissionEntity : rolePermissionList) {
//            permissions.add(permissionDao.selectById(rolePermissionEntity.getPermissionId()));
//        }
//        //排序
//        Stream<PermissionEntity> sortedPermissions = permissions.stream().sorted(Comparator.comparing(PermissionEntity::getId));
//
//        //获取部门信息
//        List<UserDeptEntity> userDeptEntities = userDeptDao.selectList(new QueryWrapper<UserDeptEntity>().eq("user_id", userId));
//        List<DeptEntity> depts = new LinkedList<>();
//        for (UserDeptEntity userDeptEntity : userDeptEntities) {
//            depts.add(deptDao.selectById(userDeptEntity.getDeptId()));
//        }
        //ToDo 头像url
        String imgUrl = "https://www.ustb.edu.cn/images/content/2017-04/20170418142450153379.png";

        //返回内容
//        HashMap<String, Object> res = new HashMap<>();
//        res.put("name", TokenUtil.getName(request));
//        res.put("avatar", imgUrl);
//        res.put("roles", roles);
//        res.put("permissions", sortedPermissions);
//        res.put("depts", depts);
        return R.ok("获取信息成功").put("data", imgUrl);


    }
    @Override
    public String queryUserNameWithUserId(String UserId) {
        SysUser sysUser =  sysUserMapper.selectUserById(Long.parseLong(UserId));
        return sysUser.getUserName();
    }

    @Override
    public String queryNickNameWithUserId(String UserId) {
        SysUser sysUser =  sysUserMapper.selectUserById(Long.parseLong(UserId));
        return sysUser.getNickName();
    }

    @Override
    public List<SysUser> queryWithDeptId(Long dept_id) {
        List<SysUser> deptUsers= sysUserMapper.selectMessageBydeptId(dept_id);
        System.out.println(deptUsers);
        return deptUsers;
    }
}
